package com.dubbo.yewu.remote;

import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.yewu.contract.IBusinessLogicService;
import com.dubbo.yewu.entity.ListingItem;
import com.dubbo.platform.product.entity.Product;
import com.dubbo.platform.user.entity.User;
import com.dubbo.yewu.system.init.RPCClients;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 */


@Service(version = "1.0.0")
public class BusinessLogicServiceImpl implements IBusinessLogicService {

    @Autowired
    RPCClients dubboRemoteSerivces;


    public List<ListingItem> getProductListing(Map<String, String> param)
    {
        //获取商品列表
        List<Product> products = dubboRemoteSerivces.getSyncProductService().getProductsByTimestamp(Integer.parseInt(param.get("type")));

        Product tmp = dubboRemoteSerivces.getSyncProductService().getProductsById(1L);
        tmp.getInfoId();
        tmp.getDescribe();

        List<Long> uids = new ArrayList<Long>();
        Map<Long, Product> uidProMap = new HashMap<Long, Product>();

        //遍历结果获取uid
        for(Product item : products){
            uids.add(item.getUid());
            uidProMap.put(item.getUid(), item);
        }

        //批量获取用户信息
        List<User> users = dubboRemoteSerivces.getSyncUserService().getUserInfos(uids);

        //构造返回数据
        List<ListingItem> Listing = new ArrayList<ListingItem>();
        for(User user: users){
            ListingItem item = new ListingItem();
            Product tmpPro = uidProMap.get(user.getUid());
            item.setInfoId(tmpPro.getInfoId());
            item.setTitle(tmpPro.getTitle());
            item.setDescribe(tmpPro.getDescribe());
            item.setImages(tmpPro.getImages());
            item.setUid(user.getUid());
            item.setNickName(user.getNickname());
            Listing.add(item);
        }
        return Listing;
    }
}
